package com.test.ftp;

import it.sauronsoftware.ftp4j.FTPAbortedException;
import it.sauronsoftware.ftp4j.FTPClient;
import it.sauronsoftware.ftp4j.FTPDataTransferException;
import it.sauronsoftware.ftp4j.FTPDataTransferListener;
import it.sauronsoftware.ftp4j.FTPException;
import it.sauronsoftware.ftp4j.FTPFile;
import it.sauronsoftware.ftp4j.FTPIllegalReplyException;
import it.sauronsoftware.ftp4j.FTPListParseException;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

public class FtpTestMain {

	public static String reply;
	static FTPClient client;
	static long fileTransfered = 0, aa = 0;

	/**
	 * @param args
	 */
	public static void main(String[] args) {

		client = new FTPClient();
		try {
			// client.connect("002.3vftp.com", 21);
			// client.login("lllkey", "liushuaiqi");
			// client.connect("192.168.1.103", 8023);
			// client.login("d", "d");
			client.connect("192.168.1.104", 8027);
			client.login("ftp", "ftp");
			String dir = client.currentDirectory(); // 显示当前目录
			System.out.println(dir);
			// 修改进入的目录
			client.changeDirectory("/Users/leewolf/Downloads");
			// FTPFile ftpfile[];
			// try {
			// ftpfile = client.list();
			// System.out.println(ftpfile.length);
			// for (FTPFile ftpFile2 : ftpfile) {
			// System.out.println(ftpFile2.getName());
			// }
			// } catch (IllegalStateException | IOException
			// | FTPIllegalReplyException | FTPException
			// | FTPDataTransferException | FTPAbortedException
			// | FTPListParseException e) {
			// e.printStackTrace();
			// }

			// (new FtpTestMain()).run();
			// client.upload(new
			// File("/Users/leewolf/Desktop/i/Chinese Painting.docx"));
			Thread t = new Thread(new Runnable() {

				@Override
				public void run() {
					try {
						System.out.println("start 1");
						Thread t1 = new Thread(new Runnable() {

							@Override
							public void run() {
								try {
									Thread.sleep(13000);
									client.abortCurrentDataTransfer(true);
								} catch (InterruptedException e) {
									// TODO Auto-generated catch block
									e.printStackTrace();
								} catch (IOException e) {
									// TODO Auto-generated catch block
									e.printStackTrace();
								} catch (FTPIllegalReplyException e) {
									// TODO Auto-generated catch block
									e.printStackTrace();
								}
							}
						});
						t1.start();
						try {
							Thread.sleep(10000);
							client.upload(new File(
									"/Users/leewolf/Desktop/i/test.rm"),
									new FTPDataTransferListener() {

										@Override
										public void transferred(int arg0) {
											fileTransfered += arg0;
											System.out.println("transferred: "
													+ fileTransfered);
										}

										@Override
										public void started() {
											System.out.println("started");

										}

										@Override
										public void failed() {
											System.out.println("failed");

										}

										@Override
										public void completed() {
											System.out.println("completed");

										}

										@Override
										public void aborted() {
											System.out.println("aborted"
													+ fileTransfered);
										}
									});
						} catch (FTPAbortedException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
						System.out.println(fileTransfered);
						Thread.sleep(2000);
						System.out.println("start 2");
						try {
							InputStream is = new FileInputStream(new File(
									"/Users/leewolf/Desktop/i/test.rm"));
							is.skip(fileTransfered);
							client.append("test.rm", is, 0,
									new FTPDataTransferListener() {

										@Override
										public void transferred(int arg0) {
											fileTransfered += arg0;
											aa += arg0;

											System.out.println("transferred: "
													+ fileTransfered
													+ "   aa: " + aa);
										}

										@Override
										public void started() {
											// TODO Auto-generated method stub

										}

										@Override
										public void failed() {
											// TODO Auto-generated method stub

										}

										@Override
										public void completed() {
											// TODO Auto-generated method stub

										}

										@Override
										public void aborted() {
											// TODO Auto-generated method stub

										}
									});
							// client.append(new File(
							// "/Users/leewolf/Desktop/i/test.rm"),
							// new FTPDataTransferListener() {
							//
							// @Override
							// public void transferred(int arg0) {
							// fileTransfered += arg0;
							// System.out.println("transferred: "
							// + fileTransfered);
							// }
							//
							// @Override
							// public void started() {
							// System.out.println("started");
							//
							// }
							//
							// @Override
							// public void failed() {
							// System.out.println("failed");
							//
							// }
							//
							// @Override
							// public void completed() {
							// System.out.println("completed");
							//
							// }
							//
							// @Override
							// public void aborted() {
							// System.out.println("aborted");
							// }
							// });
							System.out.println("ok");
						} catch (FTPAbortedException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					} catch (IllegalStateException | IOException
							| FTPIllegalReplyException | FTPException
							| FTPDataTransferException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}

				}
			});
			t.start();
		} catch (IllegalStateException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (FTPIllegalReplyException e) {
			e.printStackTrace();
		} catch (FTPException e) {
			e.printStackTrace();
		}
	}

	public void run() // 监听客户端输入的指令
	{
		reply = "";
		String str = "";
		int parseResult;
		int n = 0;
		byte[] b = new byte[1024];
		try {
			// Socket csocket;
			// BufferedReader in = new BufferedReader(new InputStreamReader(
			// csocket.getInputStream()));
			// out = new PrintWriter(csocket.getOutputStream(), true);
			// state = FtpState.FS_WAIT_LOGIN;
			// Scanner scanner = new Scanner(System.in);
			n = System.in.read(b);
			// 转换为字符串
			String s = new String(b, 0, n - 1);
			boolean finished = false;
			// while (true) {
			while (!finished) {
				// str = in.readLine();
				str = s;
				System.out.println(s);
				if (str == null)
					finished = true;
				else {
					parseResult = parseInput(str);
					// System.out
					// .println("Command:" + conCmd + " Parameter:" + param);
					System.out.print("->");
					// switch (state) {
					// case FtpState.FS_WAIT_LOGIN:
					// finished = commandUSER();
					// break;
					// case FtpState.FS_WAIT_PASS:
					// finished = commandPASS();
					// break;
					// case FtpState.FS_LOGIN: {
					switch (parseResult) {
					case -1:
						errCMD();
						break;
					case 27:
						finished = commandList();
						break;
					case 4:
						finished = commandCDUP();
						break;
					case 6:
						finished = commandCWD();
						break;
					case 7:
						finished = commandQUIT();
						break;
					case 9:
						finished = commandQUIT();
						break;
					case 11:
						finished = commandTYPE();
						break;
					case 14:
						finished = commandRETR();
						break;
					case 15:
						finished = commandSTOR();
						break;
					case 26:
					case 33:
						finished = commandPWD();
						break;
					case 32:
						finished = commandNOOP();
						break;
					case 22:
						finished = commandABOR();
						break;
					case 23:
						finished = commandDELE();
						break;
					case 25:
						finished = commandMKD();
						break;
					}// switch(parseResult) end
						// }// case FtpState.FS_LOGIN: end
						// break;

					// }// switch(state) end
				} // else
				System.out.println(finished);
				System.out.println(reply);
				// out.println(reply);
			} // while
		} // try
		catch (Exception e) {
			e.printStackTrace();
		}
	}

	private boolean commandList() {
		FTPFile ftpfile[];
		try {
			ftpfile = client.list();
			System.out.println(ftpfile.length);
			for (FTPFile ftpFile2 : ftpfile) {
				System.out.println(ftpFile2.getName());
			}
		} catch (IllegalStateException | IOException | FTPIllegalReplyException
				| FTPException | FTPDataTransferException | FTPAbortedException
				| FTPListParseException e) {
			e.printStackTrace();
		}
		return false;
	}

	private boolean commandPASS() {
		// TODO Auto-generated method stub
		return false;
	}

	private boolean commandUSER() {
		// TODO Auto-generated method stub
		return false;
	}

	private boolean commandMKD() {
		// TODO Auto-generated method stub
		return false;
	}

	private boolean commandDELE() {
		// TODO Auto-generated method stub
		return false;
	}

	private boolean commandABOR() {
		// TODO Auto-generated method stub
		return false;
	}

	private boolean commandNOOP() {
		// TODO Auto-generated method stub
		return false;
	}

	private boolean commandPWD() {
		// TODO Auto-generated method stub
		return false;
	}

	private boolean commandSTOR() {
		// TODO Auto-generated method stub
		return false;
	}

	private boolean commandRETR() {
		// TODO Auto-generated method stub
		return false;
	}

	private boolean commandTYPE() {
		// TODO Auto-generated method stub
		return false;
	}

	private boolean commandQUIT() {
		// TODO Auto-generated method stub
		return false;
	}

	private boolean commandCWD() {
		// TODO Auto-generated method stub
		return false;
	}

	private boolean commandCDUP() {
		return false;
	}

	private void errCMD() {
		System.out.println("command error");
	}

	int parseInput(String s) // 当输入不同的指令时返回不同的整数值（用于调用具体方法时的识别）
	{
		String upperCmd;
		int p = 0;
		String conCmd = "";
		String conParam = "";
		p = s.indexOf(" ");
		if (p == -1)
			conCmd = s;
		else
			conCmd = s.substring(0, p); // 当输入参数不为空时把参数赋给变量conCmd

		if (p >= s.length() || p == -1)
			conParam = "";
		else
			conParam = s.substring(p + 1, s.length());
		upperCmd = conCmd.toUpperCase();

		// 根据不同的输入参数返回不同的数值，根据不同的数值再调用相映的方法

		int i = -1;
		// 命令放置在cmd变量中，参数放置在Param变量中
		if (conCmd.equals("USER"))
			i = 1;
		if (conCmd.equals("PASS"))
			i = 2;
		if (conCmd.equals("ACCT"))
			i = 3;
		if (conCmd.equals("CDUP"))
			i = 4;
		if (conCmd.equals("SMNT"))
			i = 5;
		if (conCmd.equals("CWD"))
			i = 6;
		if (conCmd.equals("QUIT")) // 退出系统
			i = 7;
		if (conCmd.equals("REIN"))
			i = 8;
		if (conCmd.equals("PORT"))
			i = 9;
		if (conCmd.equals("PASV")) // 进入被动传输方式
			i = 10;
		if (conCmd.equals("TYPE")) // 查看状态模式
			i = 11;
		if (conCmd.equals("STRU"))
			i = 12;
		if (conCmd.equals("MODE"))
			i = 13;
		if (conCmd.equals("RETR"))
			i = 14;
		if (conCmd.equals("STOR"))
			i = 15;
		if (conCmd.equals("STOU"))
			i = 16;
		if (conCmd.equals("APPE"))
			i = 17;
		if (conCmd.equals("ALLO"))
			i = 18;
		if (conCmd.equals("REST"))
			i = 19;
		if (conCmd.equals("RNFR"))
			i = 20;
		if (conCmd.equals("RNTO"))
			i = 21;
		if (conCmd.equals("ABOR"))
			i = 22;
		if (conCmd.equals("DELE")) // 删除文件
			i = 23;
		if (conCmd.equals("RMD")) // 删除目录
			i = 24;
		if (conCmd.equals("XMKD"))
			i = 25;
		if (conCmd.equals("MKD")) // 创建目录
			i = 25;
		if (conCmd.equals("PWD")) // 显示远程主机的当前工作目录
			i = 26;
		if (conCmd.equals("LIST"))
			i = 27;
		if (conCmd.equals("NLST"))
			i = 28;
		if (conCmd.equals("SITE"))
			i = 29;
		if (conCmd.equals("SYST"))
			i = 30;
		if (conCmd.equals("HELP")) // 显示ftp内部命令cmd的帮助信息，
			i = 31;
		if (conCmd.equals("NOOP"))
			i = 32;
		if (conCmd.equals("XPWD"))
			i = 33;
		if (conCmd.equals("FEAT"))
			i = 34;
		return i;
	}// parseInput end
}
